package mobile.map;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class FHMinMapView extends MapView {

	List<GeoPoint> mapCenterGeoPoints = new ArrayList<GeoPoint>();
	List<Integer> mapCenterIDs = new ArrayList<Integer>();
	List<Integer> mapCenterZoomLevels = new ArrayList<Integer>();
	GeoPoint currentCenter = null;
	int currentSite = -1;

	public FHMinMapView(android.content.Context context, android.util.AttributeSet attrs) {
		super(context, attrs);
		populateCenterList();
		changeSites();
		this.setSatellite(true);
	}

	public FHMinMapView(android.content.Context context, android.util.AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		populateCenterList();
		changeSites();
		this.setSatellite(true);
	}

	public FHMinMapView(android.content.Context context, java.lang.String apiKey) {
		super(context, apiKey);
		populateCenterList();
		changeSites();		
		this.setSatellite(true);
	}

	public void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		MapController myMC = this.getController();
		setCurrentSite(getCurrentSite());
		setCurrentCenter(mapCenterGeoPoints.get(getCurrentSite()));
		myMC.setZoom(mapCenterZoomLevels.get(getCurrentSite()));		

	}

	public void changeSites() {
		MapController myMC = this.getController();
		int siteID = getCurrentSite();
		if ((siteID < (mapCenterIDs.size() - 1)) && (siteID != -1)){
			setCurrentSite(siteID + 1);
		} else {
			setCurrentSite(0);
		}
		setCurrentCenter(mapCenterGeoPoints.get(getCurrentSite()));
		myMC.setZoom(mapCenterZoomLevels.get(getCurrentSite()));
	}
	
	public int getCurrentSite() {
		return currentSite;
	}
	
	public void setCurrentSite(int cs) {
		currentSite = cs;
	}

	public GeoPoint getCurrentCenter() {
		return currentCenter;
	}

	private void setCurrentCenter(GeoPoint cc) {
		MapController myMC = this.getController();
		currentCenter = cc;
		myMC.setCenter(cc);
	}

	private void populateCenterList() {
		@SuppressWarnings("rawtypes")
		Class arrayRes = R.array.class;
		TypedArray centeredNames = getResources().obtainTypedArray(R.array.MapCenterNames);
		Field arrayResField = null;
		int arrayResId = 0;
		for (int i = 0; i < centeredNames.length(); i++) {
			try {
				arrayResField = arrayRes.getField(centeredNames.getString(i));
			} catch (SecurityException e1) {
				e1.printStackTrace();
			} catch (NoSuchFieldException e1) {
				e1.printStackTrace();
			}
			try {
				arrayResId = arrayResField.getInt(null);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			Resources res = getResources();
			int[] myResIntArray = res.getIntArray(arrayResId);
			mapCenterGeoPoints.add(new GeoPoint(myResIntArray[0], myResIntArray[1]));
			mapCenterZoomLevels.add(myResIntArray[2]);
			mapCenterIDs.add(myResIntArray[3]);
		}
	}
}
